package com.goodluck.goodluckim.network;

import com.goodluck.goodluckim.db.model.WebSocketResult;
import com.goodluck.goodluckim.sdk.constant.MsgActions;
import com.goodluck.goodluckim.sdk.dispatcher.base.MessageContext;
import com.goodluck.goodluckim.utils.GsonUtil;

/**
 * 消息分发
 * Created by wyj on 2018/9/17.
 */

public class WebSocketDispatcher {
    private MessageContext messageContext;

    private WebSocketDispatcher() {
        messageContext = new MessageContext();
    }

    private static class InstanceHolder {
        static WebSocketDispatcher INSTANCE = new WebSocketDispatcher();
    }

    public static WebSocketDispatcher getInstance() {
        return WebSocketDispatcher.InstanceHolder.INSTANCE;
    }

    /**
     * 消息分发处理
     */
    public void dispatcher(String message) {
        WebSocketResult webSocketResult = GsonUtil.getInstance().fromJson(message, WebSocketResult.class);
        webSocketResult.setData(MsgActions.parseFrom(webSocketResult.getAction(), webSocketResult.getData().toString()));
        messageContext.dispatcher(webSocketResult);
    }
}
